#include "bsp_tm1650.h"
#include "driver/i2c.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#include "string.h"
#include "bsp_param.h"
#include "bsp_beep.h"
static i2c_config_t conf_master = {
    .mode = I2C_MODE_MASTER,
    .sda_pullup_en = GPIO_PULLUP_DISABLE,
    .scl_pullup_en = GPIO_PULLUP_DISABLE,
    .master.clk_speed = 100000,
    .sda_io_num = I2C_MASTER_SDA_PORT,
    .scl_io_num = I2C_MASTER_SCL_PORT,
    .clk_flags = 0,
};

QueueHandle_t tm1650_led_queue =NULL;
// SemaphoreHandle_t tm1650_modekey_Mutex_Semaphore = NULL;
static void i2c_master_init()
{
    i2c_param_config(I2C_MASTER_NUM, &conf_master);
    i2c_driver_install(I2C_MASTER_NUM, conf_master.mode , 0, 0, 0);
    
}

static void TM1650_Wr_RAM(uint8_t Address, uint8_t Data)
{
    i2c_cmd_handle_t handle = i2c_cmd_link_create();
    i2c_master_start(handle);
    i2c_master_write_byte(handle, Address, true);
    i2c_master_write(handle, &Data, 1, true);
    i2c_master_stop(handle);
    i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 0);
    i2c_cmd_link_delete(handle);
}
static void TM1650_poweron_ledcheck()
{
    TM1650_Wr_RAM(LED_DIG1_ADD,0xff);
    TM1650_Wr_RAM(LED_DIG2_ADD,0xff);
    TM1650_Wr_RAM(LED_DIG3_ADD,0xff);
    TM1650_Wr_RAM(LED_DIG4_ADD,0xff);
}

void TM1650_Init(void)
{
    i2c_master_init();
    tm1650_led_queue = xQueueCreate(5, DIG_MAX);
    TM1650_Wr_RAM(0x48,0x00);      
    TM1650_Wr_RAM(LED_DIG1_ADD,0);
    TM1650_Wr_RAM(LED_DIG2_ADD,0);
    TM1650_Wr_RAM(LED_DIG3_ADD,0);
    TM1650_Wr_RAM(LED_DIG4_ADD,0);
    TM1650_Wr_RAM(0x48,0x09);
    /* 全显示 */
    TM1650_poweron_ledcheck();
}


/**
 * @brief   按键板 按键状态读取
 * @return  NULL
 */
static uint8_t KEYtm_Scan(void)
{
    uint8_t Temp = KEY_NO_BUTTON_PRESS;
    static uint8_t Temp_old = KEY_NO_BUTTON_PRESS;
    i2c_cmd_handle_t handle = i2c_cmd_link_create();
    i2c_master_start(handle);
    i2c_master_write_byte(handle, 0x4f, true);
    i2c_master_read(handle, &Temp, 1, I2C_MASTER_ACK);
    i2c_master_stop(handle);
    i2c_master_cmd_begin(I2C_MASTER_NUM, handle, 0);
    i2c_cmd_link_delete(handle);
    if(Temp == KEY_NO_BUTTON_PRESS){
        return KEY_NO_BUTTON_PRESS;
    }
 
    if(Temp != Temp_old){
        Temp_old = Temp;
        return Temp;
    }
    return KEY_NO_BUTTON_PRESS;
}

/**
 * @brief   按键板led驱动
 * @param   *led_drive  选择需要驱动的led
 */
static void tm1650_led_display(uint8_t *led_drive)
{
    TM1650_Wr_RAM(LED_DIG1_ADD, led_drive[DIG1]);

    TM1650_Wr_RAM(LED_DIG2_ADD, led_drive[DIG2]);

    TM1650_Wr_RAM(LED_DIG3_ADD, led_drive[DIG3]);

    TM1650_Wr_RAM(LED_DIG4_ADD, led_drive[DIG4]);
}

uint8_t dig_buffer[DIG_MAX] = {0};

uint8_t *TM1650_datatrans(void *display_struct)
{
    sys_main_param_t *display_param = NULL;
    display_param = display_struct;
    static union dig1_t dig1_temporary = {0};
    static union dig2_t dig2_temporary = {0};
    static union dig3_t dig3_temporary = {0};
    static union dig4_t dig4_temporary = {0};


    if(display_param->sys_strength == STRENGTH_L){
        dig2_temporary.dig2_bit_t.temp_h1 = 0;
        dig2_temporary.dig2_bit_t.temp_h2 = 0;
        dig3_temporary.dig3_bit_t.temp_l1 = 1;
        dig3_temporary.dig3_bit_t.temp_l2 = 1;
    }else{
        dig2_temporary.dig2_bit_t.temp_h1 = 1;
        dig2_temporary.dig2_bit_t.temp_h2 = 1;
        dig3_temporary.dig3_bit_t.temp_l1 = 0;
        dig3_temporary.dig3_bit_t.temp_l2 = 0;
    }


    if(display_param->sys_mode == MODE_SHI){
        dig1_temporary.dig1_bit_t.shi1 = 1;
        dig1_temporary.dig1_bit_t.shi2 = 1;
        dig1_temporary.dig1_bit_t.xu1 = 0;
        dig3_temporary.dig3_bit_t.xu2 = 0;
    }else{
        dig1_temporary.dig1_bit_t.shi1 = 0;
        dig1_temporary.dig1_bit_t.shi2 = 0;
        dig1_temporary.dig1_bit_t.xu1 = 1;
        dig3_temporary.dig3_bit_t.xu2 = 1;
    }
    


    switch(display_param->sys_status){
        case SYS_RUN_NORMAL:    // 运行
            dig2_temporary.dig2_bit_t.error1 = 0;
            dig4_temporary.dig4_bit_t.error2 = 0;
            dig4_temporary.dig4_bit_t.on1 = 1;
            dig4_temporary.dig4_bit_t.on2 = 1;
            break;
        case SYS_PAUSE:
            dig2_temporary.dig2_bit_t.error1 = 0;
            dig4_temporary.dig4_bit_t.error2 = 0;
            dig4_temporary.dig4_bit_t.on1 = 0;
            dig4_temporary.dig4_bit_t.on2 = 0;
            break;
        case SYS_STOP:
            if(display_param->sys_error_type != ERROR_TYPE_NO_ERROR){
                dig2_temporary.dig2_bit_t.error1 = 1;
                dig4_temporary.dig4_bit_t.error2 = 1;
            }else{
                dig2_temporary.dig2_bit_t.error1 = 0;
                dig4_temporary.dig4_bit_t.error2 = 0;
            }

            dig4_temporary.dig4_bit_t.on1 = 0;
            dig4_temporary.dig4_bit_t.on2 = 0;
            break;
        default:
            break; 
    }

    
 
    dig_buffer[DIG1] = dig1_temporary.dig1_byte;
    dig_buffer[DIG2] = dig2_temporary.dig2_byte;
    dig_buffer[DIG3] = dig3_temporary.dig3_byte;
    dig_buffer[DIG4] = dig4_temporary.dig4_byte;
    return dig_buffer;

}




void task_tm1650(void* arg)
{
    uint8_t tm1650_key = 0;
    uint8_t led_display[DIG_MAX] = {0};
    sys_main_param_t* tm1650_sys_param = Get_sys_param_pos();
    for (;;) {  
        vTaskDelay(50 / portTICK_PERIOD_MS);
        /* led灯点亮 */
        if (xQueueReceive(tm1650_led_queue, led_display, 0)){
            tm1650_led_display(led_display);
        }
        /* 按键扫描 */
        tm1650_key = KEYtm_Scan();
        switch(tm1650_key){
            case KEY_DIG4_TEMPL_PRESS:
                beep_output(BEEP_BUTTON);
                tm1650_sys_param->sys_strength = STRENGTH_L;
                break;
            case KEY_DIG4_STATUS_PRESS:
                beep_output(BEEP_BUTTON);
                if(tm1650_sys_param->sys_status < SYS_RUN_NORMAL){
                    tm1650_sys_param->sys_status = SYS_RUN_NORMAL;
                }else if(tm1650_sys_param->sys_status >= SYS_RUN_NORMAL){
                    tm1650_sys_param->sys_status = SYS_STOP;
                }
                break;
            case KEY_DIG4_TEMPH_PRESS:
                beep_output(BEEP_BUTTON);
                tm1650_sys_param->sys_strength = STRENGTH_H;
                break;
            case KEY_DIG4_XUSHI_PRESS:

                beep_output(BEEP_BUTTON);
                if(tm1650_sys_param->sys_status >= SYS_RUN_NORMAL){
                    break;
                }
                if((++tm1650_sys_param->sys_mode) > MODE_XU){
                    tm1650_sys_param->sys_mode = MODE_SHI;
                }

                break; 
            default:
                break;
        }
    }
}


